package com.example.process.node;

;
import com.example.process.node.enums.ProcessNodeEnum;
import com.example.process.node.enums.ProcessTemplateEnum;
import com.example.process.util.SpringUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/***
 * @description: 通用模板初始化节点基类
 * @author ts2333
 * @date: 2022/6/15 15:42
 */
@Lazy
@Component
public class BaseTemplate {

    public Map<Integer, NodeProcessor> baseNodeMap = new ConcurrentHashMap<>();

    public Map<Integer, NodeProcessor> getNodeMap() {
        return baseNodeMap;
    }

    @PostConstruct
    public void init(){
        //初始化默认节点bean
        this.initBaseNodes();
        //初始化特定模板的节点bean
        for (ProcessTemplateEnum value : ProcessTemplateEnum.values()) {
            Class templateClass = value.getTemplateClass();
            Class[] declaredClasses = templateClass.getDeclaredClasses();
            Map<Integer, NodeProcessor> nodeMap = value.getNodeMap();
            nodeMap.putAll(baseNodeMap);
            for (Class declaredClass : declaredClasses) {
                NodeProcessor bean = (NodeProcessor) SpringUtil.getBean(declaredClass);
                nodeMap.put(bean.getNodeCode(),bean);
            }
        }
    }

    public void initBaseNodes(){
        for (ProcessNodeEnum value : ProcessNodeEnum.values()) {
            Class<? extends NodeProcessor> nodeClass = value.getNodeClass();
            String simpleName = nodeClass.getSimpleName();
            simpleName = simpleName.substring(0,1).toLowerCase(Locale.ROOT)+ simpleName.substring(1);
            NodeProcessor bean = (NodeProcessor)SpringUtil.getBean(simpleName);
            baseNodeMap.put(value.getNodeCode(),bean);
        }
    }

}
